package com.jeady.timetodo.activities

import android.content.Context
import android.content.pm.PackageManager
import android.hardware.Sensor
import android.hardware.SensorEvent
import android.hardware.SensorEventListener
import android.hardware.SensorManager
import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material3.Text
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.remember
import com.jeady.jxcompose.ButtonText
import com.jeady.timetodo.ui.theme.TimeToDoTheme

class SensorActivity: ComponentActivity() {
    private val TAG: String = "[SensorActivity]"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            TimeToDoTheme {
                val sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager
                LazyColumn{
                    item {
                        val gravityValue = remember{ mutableStateListOf(0f) }
                        val rotationValue = remember{ mutableStateListOf(0f) }
                        val stepValue = remember{ mutableStateListOf(0f) }
                        val gravityListener = object : SensorEventListener {
                            override fun onSensorChanged(event: SensorEvent?) {
                                Log.d(TAG, "onSensorChanged: sensorGravity  ${event?.values?.toList()}")
                                gravityValue.clear()
                                gravityValue.addAll(event?.values?.toList()?: emptyList())
                            }

                            override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {
                                Log.d(TAG, "onAccuracyChanged: sensorGravity $accuracy")
                            }
                        }
                        val rotationListener = object : SensorEventListener {
                            override fun onSensorChanged(event: SensorEvent?) {
                                Log.d(TAG, "onSensorChanged: sensorRotation  ${event?.values?.toList()}")
                                rotationValue.clear()
                                rotationValue.addAll(event?.values?.toList()?: emptyList())
                            }

                            override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {
                                Log.d(TAG, "onAccuracyChanged: sensorRotation $accuracy")
                            }
                        }
                        val stepListener = object : SensorEventListener {
                            override fun onSensorChanged(event: SensorEvent?) {
                                Log.e(TAG, "onSensorChanged: get sensorStep value ${event?.values?.toList()}",)
                                stepValue.clear()
                                stepValue.addAll(event?.values?.toList()?: emptyList())
                            }
                            override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {
                                Log.e(TAG, "onAccuracyChanged: get sensorStep value $accuracy",)
                            }
                        }
                        Column {
                            Row {
                                ButtonText("重力加速度") {
                                    val sensorGravity =
                                        sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY)
                                    sensorGravity?.let {
                                        Log.d(TAG, "onCreate: register gravity sensor")
                                        sensorManager.registerListener(
                                            gravityListener, it, 1 * 1000 * 1000 * 1000
                                        )
                                    }
                                }
                                ButtonText("取消重力加速度") {
                                    sensorManager.unregisterListener(gravityListener)
                                }
                            }
                            Text("${gravityValue.toList()}")
                            Row {
                                ButtonText("旋转加速度") {
                                    val sensorRotation =
                                        sensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR)
                                    sensorRotation?.let {
                                        Log.d(TAG, "onCreate: register sensorRotation sensor")
                                        sensorManager.registerListener(
                                            rotationListener, it, SensorManager.SENSOR_DELAY_NORMAL
                                        )
                                    }
                                }
                                ButtonText("取消旋转加速度") {
                                    sensorManager.unregisterListener(rotationListener)
                                }
                            }
                            Text("${rotationValue.toList()}")
                            Row {
                                ButtonText(
                                    "计步器 ${
                                        packageManager.hasSystemFeature(
                                            PackageManager.FEATURE_SENSOR_STEP_COUNTER
                                        )
                                    }"
                                ) {
                                    val sensorStep =
                                        sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER)
                                    sensorStep?.let {
                                        Log.d(TAG, "onCreate: register sensorStep sensor")
                                        sensorManager.registerListener(
                                            stepListener, it, SensorManager.SENSOR_DELAY_NORMAL
                                        )
                                    }
                                }
                                ButtonText("取消计步") {
                                    sensorManager.unregisterListener(stepListener)
                                }
                            }
                            Text("${stepValue.toList()}")
                        }
                    }
                    item{
                        Column {
                            val tempList = remember{ mutableStateListOf(0f) }
                            Row {
                                val tempListener = object: SensorEventListener{
                                    override fun onSensorChanged(event: SensorEvent?) {
                                        tempList.clear()
                                        tempList.addAll(event?.values?.toList()?: emptyList())
                                    }

                                    override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {
                                        TODO("Not yet implemented")
                                    }

                                }
                                ButtonText(
                                    "温度 ${
                                        packageManager.hasSystemFeature(
                                            PackageManager.FEATURE_SENSOR_LIGHT
                                        )
                                    }"
                                ) {
                                    val sensorStep =
                                        sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT)
                                    sensorStep?.let {
                                        Log.d(TAG, "onCreate: register sensorStep sensor")
                                        sensorManager.registerListener(
                                            tempListener, it, SensorManager.SENSOR_DELAY_NORMAL
                                        )
                                    }
                                }
                                ButtonText("取消计步") {
                                    sensorManager.unregisterListener(tempListener)
                                }
                            }
                            Text("${tempList.toList()}")
                        }
                    }
                }
            }
        }
    }
}